# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:

from MysqlTools import MysqlTools
import json

class NewVinInfo:

    def __init__(self, **kwargs):
        self.db = MysqlTools(db_config=kwargs.get("db_config"), type='dict', auto_commit=1000)
        self.vin_table = kwargs.get("vin_table")

    def run(self):
        sql = 'select * from `{}`'.format(self.vin_table)
        data = self.db.select_all(sql)
        _l = []
        for info in data:
            try:
                extra_info = json.loads(info.get("extra_info"))
            except :
                print(info.get("vin"))
                print(info.get("extra_info"))
                exit()
            except_ = extra_info.get("except")
            should_ = extra_info.get("should")
            new_except = set(map(lambda x: '不带'+x, except_))
            should_.extend(new_except)
            extra_info.pop("except")
            extra_info.pop("should")
            extra_info["app_info"] = should_
            extra_info["sa"] = []
            info['extra_info'] = json.dumps(extra_info, ensure_ascii=False)
            info['updatetime'] = 'now()'
            info['createtime'] = 'now()'
            insql = self.db.get_insert_sql('cars_vin_info_new', info)
            self.db.execute_sql(insql)
        self.db.commit_sql()

    def construct_sa_desc_dict(self, table):
        sql = '''select * from `{}`'''.format(table)
        data = self.db.select_all(sql)
        _dict = {}
        for info in data:
            sa = info.get("sa")
            desc = info.get("desc")
            _dict[desc] = sa
        return _dict

    def get_sa(self, list):
        _l = []
        for i in list:
            sa = self.sa_desc_dict.get(i, "")
            if sa == '':
                continue
            _l.append(sa)
        return _l

    def format_vin_ext(self, extra_info):
        range = extra_info.get("range")
        OPR = range.split(" ")[0] if range.split(" ") else ""
        app_info = extra_info.get("app_info") if extra_info["app_info"] else []
        sa = self.get_sa(app_info)
        filter_rules = {"OPR": OPR, "sa": sa}
        extra_info["filter_rules"] = filter_rules
        return extra_info

    def run_vin(self):
        self.sa_desc_dict = self.construct_sa_desc_dict('sa_desc_merge')
        sql = '''select * from `{}`;'''.format(self.vin_table)
        data = self.db.select_all(sql)
        for info in data:
            vin = info.get("vin")
            extra_info = json.loads(info.get("extra_info"))
            new_extra_info = self.format_vin_ext(extra_info)
            upsql = '''update `{}` set `extra_info`='{}' where `vin`="{}"'''.format(self.vin_table, json.dumps(new_extra_info, ensure_ascii=False), vin)
            self.db.execute_sql(upsql)
        self.db.commit_sql()

    def linshicheck(self):
        '''临时用, 删除一些无法过滤的条件'''
        sql = '''select distinct * from cars_structure_collection_new where description LIKE "%直径___厚度%";'''
        data = self.db.select_all(sql)
        num =1
        for info in data:
            id = info.get("id")
            extra_info = json.loads(info.get("extra_info"))
            filter_rules = extra_info.get("filter_rules")
            for rule in filter_rules:
                if num == 1:
                    shield = rule['app_sa'][1]
                    rule['app_sa'] = rule['app_sa'][:1] if len(rule['app_sa']) > 1 else []
                    rule['shield'] = shield
                else:
                    shield = rule['app_sa'][0]
                    rule['app_sa'] = rule['app_sa'][1:] if len(rule['app_sa']) > 1 else []
                    rule['shield'] = shield
            num += 1
            extra_str = json.dumps(extra_info, ensure_ascii=False)
            up_sql = '''update `cars_structure_collection_new` set `extra_info`='{}' where `id`="{}";'''.format(extra_str, id)
            self.db.execute_sql(up_sql)
            print(up_sql)
        self.db.commit_sql()
if __name__ == '__main__':
    db_config = dict(
        host="192.168.10.166",
        port=3306,
        user="a",
        passwd="110",
        db="peugeot",
        charset="utf8",
    )
    vin_table = 'cars_vin_info_new'
    tools = NewVinInfo(db_config=db_config, vin_table=vin_table)
    # tools.run()
    # tools.run_vin()
    tools.linshicheck()